package com.resou.reader.bookshelf.dialogutil;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.resou.reader.R;
import com.resou.reader.bookdetail.BookDetailActivity;
import com.resou.reader.bookshelf.HomeBookShelfPresenter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class BookLongPressDialog extends DialogFragment implements View.OnClickListener {
    public static final String BOOK_ID = "book_id";
    @BindView(R.id.tv_topping)
    TextView tvTopping;
    @BindView(R.id.tv_share)
    TextView tvShare;
    @BindView(R.id.tv_book_detail)
    TextView tvBookDetail;
    @BindView(R.id.tv_delete)
    TextView tvDelete;
    Unbinder unbinder;
    private HomeBookShelfPresenter mPresenter;

    public static BookLongPressDialog newInstance(String bookId) {
        Bundle args = new Bundle();
        args.putString(BOOK_ID, bookId);
        BookLongPressDialog fragment = new BookLongPressDialog();
        fragment.setArguments(args);
        return fragment;
    }

    private Context mContext;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Window window = getDialog().getWindow();
        if (window != null) {
            WindowManager.LayoutParams params = window.getAttributes();
            params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
            params.windowAnimations = R.style.bottomSheet_animation;
            getDialog().getWindow().setAttributes(params);
            getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
            getDialog().setCanceledOnTouchOutside(true);
            getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        View view = inflater.inflate(R.layout.dialog_book_press, container, false);
        unbinder = ButterKnife.bind(this, view);
        tvTopping.setOnClickListener(this);
        tvBookDetail.setOnClickListener(this);
        tvDelete.setOnClickListener(this);
        tvShare.setOnClickListener(this);
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onClick(View v) {
        if (getArguments() != null) {
            String bookId = getArguments().getString(BOOK_ID);
            if (v == tvBookDetail) {
                BookDetailActivity.startDetailActivity(mContext, bookId);
            }
            if (v == tvDelete) {
                mPresenter.deleteBook(bookId);
            }
            dismiss();
        }
    }

    public void setPresenter(HomeBookShelfPresenter presenter) {
        mPresenter = presenter;
    }

}
